package com.qqonline.mpf;


import com.qqonline.conmon.AsyncImageTask;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class UserAdapter implements ListAdapter {
	public MainActivity activity;
	public LayoutInflater mInflater;
	public int po;
	private Display display;
	public UserAdapter(MainActivity mainActivity,Display display) {
		this.activity = mainActivity;
		this.display=display;
		this.mInflater  = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub

	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getCount() {
		return activity.userList.size();
	}

	@Override
	public Object getItem(int position) {
		return activity.userGridView.getItemAtPosition(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
        if (convertView != null) {
                view = convertView;
        } else {
                view = mInflater.inflate(R.layout.usergriditem, null);
        }
        ImageView img= (ImageView) view.findViewById(R.id.userImage);
        TextView txtitle = (TextView) view.findViewById(R.id.userName);
        String userImage = activity.userList.get(position).getFirstPicture().getPicUrl();
        String userName = activity.userList.get(position).getName();
        txtitle.setText(userName);
        userImageClick cl = new userImageClick(position);
        img.setOnClickListener(cl);
        txtitle.setOnClickListener(cl);
        AsyncImageTask task = new AsyncImageTask(img,activity,display);
        task.execute(userImage);
		return view;
	}
	public class userImageClick implements  OnClickListener{
		public int position;
		public userImageClick(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("OpenId", activity.userList.get(position).getOpenId());
			intent.putExtras(bundle);
			intent.setClassName(activity, "com.qqonline.mpf.PicActivity");
			activity.startActivityForResult(intent, 100);
		}
		
	}
	@Override
	public int getItemViewType(int position) {
		return 0;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public boolean isEmpty() {
		if(activity.userList.size()<1){
		return false;
		}
		return true;
	}

	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		return false;
	}

}
